package com.michael.study.jetpack.module.login

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.michael.study.jetpack.MainActivity
import com.michael.study.jetpack.R
import com.michael.study.jetpack.base.BaseActivity
import com.michael.study.jetpack.bean.UserInfo
import com.michael.study.jetpack.databinding.ActivityLoginBinding
import com.michael.study.jetpack.network.Api
import com.michael.study.jetpack.network.ApiResult
import com.michael.study.jetpack.network.api.LoginApi
import com.michael.study.jetpack.network.body.LoginBody
import com.michael.study.jetpack.utils.Actions
import kotlinx.android.synthetic.main.activity_login.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.await
import java.lang.Exception

/**
 * Description:
 *
 * Created by liuguoquan on 2020/3/19 17:03.
 */
class LoginActivity : BaseActivity(), View.OnClickListener {

    private lateinit var mBinding: ActivityLoginBinding
    private lateinit var mModel: LoginViewModel
    private var mCondition = LoginCondition()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mModel = ViewModelProvider(this).get(LoginViewModel::class.java)
        mBinding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        btn_login.setOnClickListener(this)

        mCondition.userName = "18717172486"
        mCondition.password = "123456"

        mModel.user.observe(this, Observer {
            run {
                if (it.code == 1) {
                    Actions.startActivity(this, MainActivity::class.java)
                } else {
                    showToast(it.msg)
                }
            }
        })
    }

    override fun onClick(v: View?) {
        if (v?.id == R.id.btn_login) {
            mModel.loginCoroutineScope(mCondition)
        }
    }

    suspend fun login(userName: String, password: String){
        try {
            val body = LoginBody()
            body.username = userName
            body.password = password
            val result = Api.create<LoginApi>().login(body).await();
            //服务器回调数据做出来
        } catch (e : Exception) {
            //异常处理

        }
    }
}

